package nancal.mp.service

import nancal.mp.db.mongo.entity.log.EmailCodeLog
import nancal.mp.db.mongo.mor
import nancal.mp.mvc.sys.SendMailController.MailVo
import nbcp.base.comm.JsonResult
import nbcp.base.extend.AsString
import org.springframework.stereotype.Service

/**
 * @Author: Cuiyong
 * @Date: 2021/9/6 11:57
 */
@Service
class MyEmailService {
    @Throws(Exception::class)
    fun sendTextMail(vo: MailVo): JsonResult {
        val myEmail = nancal.mp.mvc.util.MyEmail.subject(vo.title)
            .from(vo.from)
            .to(vo.to)
            .text(vo.text)
        if (vo.attachUrlMap != null && vo.attachUrlMap.size > 0) {
            myEmail.attachUrlBatch(vo.attachUrlMap)
        }
        myEmail.send()

        var emailLog = EmailCodeLog();
        emailLog.content = vo.text.AsString(vo.html)
        emailLog.subject = vo.title
        emailLog.emails = mutableListOf(vo.to);
        mor.log.emailCodeLog.doInsert(emailLog);
        return JsonResult()
    }

    @Throws(Exception::class)
    fun sendHtml(vo: MailVo): JsonResult {
        val myEmail = nancal.mp.mvc.util.MyEmail.subject(vo.title)
            .from(vo.from)
            .to(vo.to)
            .html(vo.html)
        if (vo.attachUrlMap != null && vo.attachUrlMap.size > 0) {
            myEmail.attachUrlBatch(vo.attachUrlMap)
        }
        myEmail.send()

        var emailLog = EmailCodeLog();
        emailLog.content = vo.html.AsString(vo.text)
        emailLog.subject = vo.title
        emailLog.emails = mutableListOf(vo.to);
        mor.log.emailCodeLog.doInsert(emailLog);
        return JsonResult()
    }
}